package cntv.player.media.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import cntv.player.core.util.LogUtil;
import cntv.player.media.player.IMediaPlayer;
import cntv.player.media.player.IMediaPlayer.OnSeekCompleteListener;
import cntv.player.media.player.IMediaPlayer.OnVideoSizeChangedListener;
import cntv.player.media.player.IMediaPlayer.OnBufferingUpdateListener;
import cntv.player.media.player.IMediaPlayer.OnInfoListener;



/*public void setAnchorView(ViewGroup view) ;
public void setMediaPlayer(CntvPlayerControl cn.player);
public void show();
public void setFileName(String name);
public void show(int timeout);
public boolean isShowing();
public void hide();
public void setEnabled(boolean enabled);*/

public abstract class BaseMediaController extends FrameLayout{
	
	private static final String TAG = "zl";
	
	protected KooVideoView mVideoView;
	protected CntvPlayerControl mPlayer;
	protected boolean mIsVideoPlay;
	protected boolean mIsFirstPlay = true; 
	protected boolean mIsSeekComplete = true;
	protected OnVideoPlayListener mOnVideoPlayListener;
	
	private OnVideoSizeChangedListener mOnVideoSizeChangedListener;
	private OnSeekCompleteListener mOnSeekCompleteListener;
	private OnInfoListener mOnInfoListener;
	private OnBufferingUpdateListener mOnBufferingUpdateListener;
	
	
	
	public BaseMediaController(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}
	
	public BaseMediaController(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	public BaseMediaController(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}
	
	
	protected abstract void setAnchorView(ViewGroup view);

	protected abstract boolean isShowing();

	protected abstract void show();

	protected abstract void show(int timeout);

	protected abstract void setFileName(String name);

	protected abstract void hide();
	
	
	/**
	 * 当播放器调用stopPlayback()方法时，恢复相关初始化数据
	 */
	public void initialize(){
		mIsVideoPlay = false;
		mIsFirstPlay = true;
	}
	
	public void setSeekComplete(boolean isSeeckComplete){
		mIsSeekComplete = isSeeckComplete;
	}
	
	
	public void setMediaPlayer(CntvPlayerControl player) {
		mPlayer = player;
//		updatePauseButton();
		if (mPlayer instanceof KooVideoView){
			mVideoView = (KooVideoView) mPlayer;
		}
		setPlayerListeners();
		
		
	}
	protected void setPlayerListeners(){
		
		mOnVideoSizeChangedListener = new OnVideoSizeChangedListener() {
			
			@Override
			public void onVideoSizeChanged(IMediaPlayer mp, int width,
					int height, int sar_num, int sar_den) {
				// TODO Auto-generated method stub
				
				//本来可以直接在onInfo中处理播放逻辑，但经过测试发现，
				//首次播放时，有调用不到onInfo方法的情况，但onVideoSizeChanged方法一定能调用到
				if(mIsSeekComplete && mIsFirstPlay && mOnVideoPlayListener != null){
					mIsVideoPlay = true;
					mIsFirstPlay = false;
					mPlayer.start();
					mOnVideoPlayListener.onVideoPlay();  
					LogUtil.i(TAG,"BaseMediaController onVideoSizeChanged... mOnVideoPlayListener.onVideoPlay()");
				}
				
				if(isShowing()){//如果控制器显示了，重新显示，如果没有显示，不作操作
					show();
				}
			}
		};
		
		mOnSeekCompleteListener = new OnSeekCompleteListener() {
			
			@Override
			public void onSeekComplete(IMediaPlayer mp) {
				// TODO Auto-generated method stub
				mIsSeekComplete = true;
				LogUtil.i(TAG,"BaseMediaController onSeekComplete + percent = " + mVideoView.getBufferPercentage());
			}
		};
		
		mOnInfoListener = new OnInfoListener() {

			@Override
			public boolean onInfo(IMediaPlayer mp, int what, int extra) {
				// TODO Auto-generated method stub
				if(mIsFirstPlay){
					mIsFirstPlay = false;
					return true;
				}
				switch (what) {
				case IMediaPlayer.MEDIA_INFO_BUFFERING_START:
					LogUtil.i(TAG,"MEDIA_INFO_BUFFERING_START " );
					if(mOnVideoPlayListener != null){
						mIsVideoPlay = false;
						mPlayer.pause();
						mOnVideoPlayListener.onVideoLoading();  
					}
					break;
					
				case IMediaPlayer.MEDIA_INFO_BUFFERING_END:
					if(mIsSeekComplete && mOnVideoPlayListener != null){
						mIsVideoPlay = true;
						mPlayer.start();
						mOnVideoPlayListener.onVideoPlay();  
						LogUtil.i(TAG,"BaseMediaController MEDIA_INFO_BUFFERING_END... mOnVideoPlayListener.onVideoPlay()");
					}
					
					if(isShowing()){//如果控制器显示了，重新显示，如果没有显示，不作操作
						show();
					}
					break;
				default:
					break;
				}
				return true;
			}
		};
		
		mOnBufferingUpdateListener = new OnBufferingUpdateListener() {
			
			@Override
			public void onBufferingUpdate(IMediaPlayer mp, int percent) {
				// TODO Auto-generated method stub
				
			}
		};

		
		if(mVideoView != null){
			mVideoView.setOnVideoSizeChangedListener(mOnVideoSizeChangedListener);
			mVideoView.setOnBufferingUpdateListener(mOnBufferingUpdateListener);
			mVideoView.setOnInfoListener(mOnInfoListener);
			mVideoView.setOnSeekCompleteListener(mOnSeekCompleteListener);
		}
	}
	
	
	public interface OnVideoPlayListener {
		public void onVideoLoading();
		public void onVideoPlay();
		public void onVideoPlayComplete();
	}

	public void setOnVideoPlayListener(OnVideoPlayListener mVideoPlayListener) { 
		this.mOnVideoPlayListener = mVideoPlayListener; 
	}
	
	

	
}
